﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EntityInterfaces;

namespace DAL
{
    public class Product : IProduct
    {
        private product entity;
        private shopnetEntities model;
        private special_offers offer;

        public int id {
            get {
                return entity.productID;
            }
        }
        public int category_id {
            get {
                return (int)entity.categoryID;
            }
            set {
                entity.categoryID = value;
            }
        }
        public String name
        {
            get
            {
                return (String)entity.name;
            }
            set
            {
                entity.name = value;
            }
        }
        public String description
        {
            get
            {
                return (String)entity.description;
            }
            set
            {
                entity.description = value;
            }
        }
        public Single cust_rating {
            get {
                return entity.customers_rating;
            }
            set {
                entity.customers_rating = value;
            }
        }

        public Single price
        {
            get
            {
                return (Single)entity.Price;
            }
            set
            {
                entity.Price = value;
            }
        }

        public String picture
        {
            get
            {
                return (String)entity.picture;
            }
            set
            {
                entity.picture = value;
            }
        }

        public int in_stock {
            get {
                return (int)entity.in_stock;
            }
            set {
                entity.in_stock = value;
            }
        }

        public String thumbnail
        {
            get
            {
                return (String)entity.thumbnail;
            }
            set
            {
                entity.thumbnail = value;
            }
        }

        public int customer_votes
        {
            get
            {
                return (int)entity.customer_votes;
            }
            set
            {
                entity.customer_votes = value;
            }
        }

        public String big_picture
        {
            get
            {
                return (String)entity.big_picture;
            }
            set
            {
                entity.big_picture = value;
            }
        }

        public bool enabled
        {
            get {
                return entity.enabled != 0;
            }
            set {
                entity.enabled = value ? 1 : 0;
            }
        }

        public String brief_description
        {
            get
            {
                return (String)entity.brief_description;
            }
            set
            {
                entity.brief_description = value;
            }
        }



        public int items_sold
        {
            get
            {
                return (int)entity.items_sold;
            }
            set
            {
                entity.items_sold = value;
            }
        }

        public Single list_price
        {
            get
            {
                return (Single)entity.list_price;
            }
            set
            {
                entity.list_price = value;
            }
        }

        public String product_code
        {
            get
            {
                return (String)entity.product_code;
            }
            set
            {
                entity.product_code = value;
            }
        }
        

        public Product() {
            model = new shopnetEntities();
        }
        public void Save() {
            model.products.AddObject(entity);
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception) {
                //log
                throw new EntityException.ProductException("Product Save error",exception);
            }
        }
        public void Read(int id) {
            try
            {
                entity = model.products.Single(s=> s.productID == id);
            }
            catch (Exception exception) { 
                //log
                throw new EntityException.ProductException("Product Read error",exception);
            }

            try
            {
                offer = model.special_offers.Single(s => s.productID == id);
            }
            catch (Exception exception)
            { 
                //log    
                throw new EntityException.ProductException("Special Offers Read error", exception);
            }
        }
        public void Commit() {
            try
            {
                model.SaveChanges();
            }
            catch (Exception exception) {
                //log
                throw new EntityException.ProductException("Product Commit error", exception);
            }
        }
        public void SetAsOffer(int sorter) {
            var offer = new special_offers();
            offer.productID = id; offer.sort_order = sorter;
            model.special_offers.AddObject(offer);
            Commit();
        }
    }
}
